import random
import uuid
from io import BytesIO
import random
from django.db.models import Q
from django.shortcuts import render
from django.utils.baseconv import base64
from django.core.serializers import serialize

from utils.image_code import check_code

# Create your views here.
from stw_app import models
from stw_app.models import Order
from stw_app.models import UserInfo
from stw_app.models import Code
from django.shortcuts import render, redirect
from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from math import radians, sin, cos, sqrt, atan2
from datetime import datetime
import requests

def give_place_name(longitude, latitude):
    KEY = 'af5035a414311145ba566b1d4d9cbab0'
    LOCATION = str(longitude) + ',' + str(latitude)
    RADIUS = 3000
    OUTPUT = 'json'
    EX = 'all'
    POI_type = ''
    url = 'https://restapi.amap.com/v3/geocode/regeo'
    params = {
        "location": LOCATION,
        "output": OUTPUT,
        "key": KEY,
    }
    response = requests.get(url, params=params)
    # answer = json.loads(response.content)
    # 这种方式也可以
    answer = response.json()
    return answer['regeocode']['formatted_address']
def get_place_name(request):
    if request.method == "POST":
        latitude = float(request.POST.get('latitude'))
        longitude = float(request.POST.get('longitude'))
        place_name=give_place_name(longitude,latitude)
        return JsonResponse({'success': True,'data':place_name})
    else:
        return JsonResponse({'success': False})